<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginFormValidate"
        >
          <h1>欢迎</h1>
          <h2>{{ setting.title }}</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              type="password"
              v-model="loginForm.password"
              show-password
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              class="login_btn"
              type="primary"
              size="default"
              @click="login"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from "@element-plus/icons-vue";
import { reactive, ref } from "vue";
import { ElNotification } from "element-plus";
//引入用户小仓库
import useUserStore from "@/store/modules/user";
//引入路由器
import { useRouter, useRoute } from "vue-router";
//引入工具方法
import { getTimeMsg } from "@/utils/time.ts";
//引入项目配置
import setting from "@/setting";

let loginForm = reactive({ username: "admin", password: "atguigu123" });

//自定义密码验证规则，由于使用了真实接口，这里就不能只填写数字了，暂时注释
//@ts-ignore
// const validatorPassword = (rule: any, value: any, callback: any) => {
//   if (/^\d{5,20}$/.test(value)) {
//     callback();
//   } else {
//     callback(new Error("密码长度5-20个数字"));
//   }
// };

let rules = {
  username: [
    { required: true, message: "必须填写用户名", trigger: "blur" },
    { min: 5, message: "用户名不能小于5个字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "必须填写密码", trigger: "blur" },
    // { min: 6, message: '密码不能小于6个字符', trigger: 'blur' }
    // 使用自定义校验规则
    // { trigger: "blur", validator: validatorPassword },
  ],
};
let userStore = useUserStore();
let $router = useRouter();
let $route = useRoute();
let loading = ref(false);
//获取要验证的表单，位于模板中ref="loginFormValidate"
let loginFormValidate = ref();

const login = async () => {
  //校验表单
  await loginFormValidate.value.validate();
  loading.value = true;
  try {
    await userStore.userLogin(loginForm);
    ElNotification({
      type: "success",
      message: "欢迎回来",
      title: `Hi,${getTimeMsg()}好`,
    });
    $router.push({ path: $route.query.redirectUrl || "/", replace: true });
    loading.value = false;
  } catch (error) {
    loading.value = false;
    ElNotification({
      type: "error",
      message: (error as Error).message,
    });
  }
};
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url("@/assets/images/background.jpg") no-repeat;
  background-size: cover;

  .login_form {
    margin-top: 30vh;
    background: url("@/assets/images/login_form.png") no-repeat;
    background-size: cover;
    padding: 20px 40px;

    h1 {
      padding: 10px 0px;
      color: white;
    }

    h2 {
      padding: 20px 0px;
      color: white;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
